package com.kdtech.analyse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.text.Summary.ChangeCode;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

public class JSoupUtils {

	/**
	 * 用指定关键字，匹配作者
	 * 用法: matchAuthor(doc,"责任编辑：","来源：")
	 * @param doc
	 * @param str
	 * @return
	 */
	public static String matchAuthor(final Element doc, String... str){
		String html = doc.html();
		String htmlDBC = StringUtils.ToDBC(html); //半角
		htmlDBC = ChangeCode.toSimple(htmlDBC); //半角
		Document dbcDoc = Jsoup.parse(htmlDBC);
		for (String string : str) {
			string = StringUtils.ToDBC(string);
			string = ChangeCode.toSimple(string);
			Elements e = dbcDoc.select(":containsOwn("+string+")");
			for (Element element : e) {
				String text = element.text();
				if (!StringUtils.isBlank(text)){
					text = decodeAuthor(text,string);
					if (text!=null){
						return text;
					}
				}
			}

			for (Element element : e) {
				Element parent = element;
				for (int i = 0; i < 3; i++) {
					parent = parent.parent();
					if (parent==null){
						break;
					}
					String text = parent.text();
					text = decodeAuthor(text,string);
					if (text!=null){
						return text;
					}
				}
			}
		}
		return null;
	}


	private static String decodeAuthor(String text, String string) {
		text = StringUtils.replaceAllToSpace(text,",",";","|","[","]","】","【","-","—","(",")","《","》","·");
		text = StringUtils.substringAfter(text, string);
		text = StringUtils.trimLeft(text);
		text = StringUtils.substringBeforeWhiteSpace(text);

		if (StringUtils.isNotBlank(text) && text.length()<20 ){
			if (!text.matches(".*[\\pP‘’“”<>]+.*")){
				return text;
			}
		}
		return null;
	}

	public static String select(Document doc, String... selects) {
		for (String string : selects) {
			String text = doc.select(string).text();
			if (!StringUtils.isBlank(text)){
				return text;
			}
		}
		return null;
	}
	public static String selectContent(String url,Document doc, String... selects) {
		for (String string : selects) {
			Elements select = doc.select(string);
			String text = select.text();
			if (!StringUtils.isBlank(text)){
				return HtmlCleaner.getContentHtml(url ,select);
			}
		}
		return null;
	}

	public static String selectContainsOwn(Document doc, String... containTexts) {
		for (String string : containTexts) {
			String text = doc.select(":containsOwn("+string+")").text();
			if (!StringUtils.isBlank(text)){
				return text;
			}
		}
		return null;
	}

	/**
		 * 从html节点中查找包含日期的节点，并从该结点中解析日期 <br>
		 * 如果该节点没有日期信息，则从父节点的文本中找日期信息 <br>
		 *<br>
		 * 如一个网站的模板中，日期节点有以下几种：<br>
		 *<br>
		 * 生成日期 	2014-01-17 <br>
		 * 时间：2013/01/22 <br>
		 * 发布时间 2012-12-21 17:51 <br>
		 *<br>
		 * 在解析类中我们可以这样使用: Long date = matchDate(doc,"生成日期" , "时间：", "发布时间"); <br>
		 *
		 * @param doc
		 * @param matchString 时间节点的标识文本，可以是多个
		 * @return
		 */
		public static Long matchDate(Document doc, String... matchStrings) {

			String html = doc.html();
			String htmlDBC = StringUtils.ToDBC(html); //半角
			Document dbcDoc = Jsoup.parse(htmlDBC);
			Long date = null;
			for (String matchString : matchStrings) {
				matchString = StringUtils.ToDBC(matchString);
				Elements select = dbcDoc.select(":containsOwn(" + matchString + ")");
				for (Element e : select) {
					String text = e.text();
					if (text!=null ){
						if (text.length()<150){
							date = DateUtils.matchDate(text);
						}else{
							date = DateUtils.matchDate(center(text, matchString,150));
						}
						if (date!=null){
							return date;
						}
					}
				}

				for (Element element : select) {
					Element parent = element;
					for (int i = 0; i < 3; i++) {
						parent = parent.parent();
						if (parent==null){
							break;
						}
						String text = parent.text();
						if (text.length()<150){
							date = DateUtils.matchDate(text);
							if (date!=null){
								return date;
							}
						}else{
							date = DateUtils.matchDate(center(text, matchString,150));
							if (date!=null){
								return date;
							}else{
								break;
							}
						}

					}
				}
			}
			return date;
		}

		private static String center(String text, String matchString, int len) {
			if (text==null || text.equals("")){
				return text;
			}
			if (text.length()<=len){
				return text;
			}

			int indexOf = text.indexOf(matchString);
			int begin = Math.max(indexOf-len/2, 0);
			int end = Math.min(matchString.length() + indexOf+len/2,text.length());
			return text.substring(begin,end);
		}


		

}
